<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.contrib.comments.models &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../../index.html" />
    <link rel="up" title="django.contrib.comments" href="../comments.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-comments-models">
            
  <h1>Source code for django.contrib.comments.models</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="kn">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.contrib.comments.managers</span> <span class="kn">import</span> <span class="n">CommentManager</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.fields</span> <span class="kn">import</span> <span class="n">GenericForeignKey</span>
<span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="kn">import</span> <span class="n">ContentType</span>
<span class="kn">from</span> <span class="nn">django.contrib.sites.models</span> <span class="kn">import</span> <span class="n">Site</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">urlresolvers</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">models</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="kn">import</span> <span class="n">ugettext_lazy</span> <span class="k">as</span> <span class="n">_</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">timezone</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">python_2_unicode_compatible</span>

<span class="n">COMMENT_MAX_LENGTH</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">settings</span><span class="p">,</span> <span class="s">&#39;COMMENT_MAX_LENGTH&#39;</span><span class="p">,</span> <span class="mi">3000</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">BaseCommentAbstractModel</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    An abstract base class that any custom comment models probably should</span>
<span class="sd">    subclass.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c"># Content-object field</span>
    <span class="n">content_type</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">ContentType</span><span class="p">,</span>
            <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;content type&#39;</span><span class="p">),</span>
            <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;content_type_set_for_</span><span class="si">%(class)s</span><span class="s">&quot;</span><span class="p">)</span>
    <span class="n">object_pk</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;object ID&#39;</span><span class="p">))</span>
    <span class="n">content_object</span> <span class="o">=</span> <span class="n">GenericForeignKey</span><span class="p">(</span><span class="n">ct_field</span><span class="o">=</span><span class="s">&quot;content_type&quot;</span><span class="p">,</span> <span class="n">fk_field</span><span class="o">=</span><span class="s">&quot;object_pk&quot;</span><span class="p">)</span>

    <span class="c"># Metadata about the comment</span>
    <span class="n">site</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Site</span><span class="p">)</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">abstract</span> <span class="o">=</span> <span class="bp">True</span>

    <span class="k">def</span> <span class="nf">get_content_object_url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a URL suitable for redirecting to the content object.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">urlresolvers</span><span class="o">.</span><span class="n">reverse</span><span class="p">(</span>
            <span class="s">&quot;comments-url-redirect&quot;</span><span class="p">,</span>
            <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">content_type_id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">object_pk</span><span class="p">)</span>
        <span class="p">)</span>


<span class="nd">@python_2_unicode_compatible</span>
<div class="viewcode-block" id="Comment"><a class="viewcode-back" href="../../../../ref/contrib/comments/models.html#django.contrib.comments.models.Comment">[docs]</a><span class="k">class</span> <span class="nc">Comment</span><span class="p">(</span><span class="n">BaseCommentAbstractModel</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A user comment about some object.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="c"># Who posted this comment? If ``user`` is set then it was an authenticated</span>
    <span class="c"># user; otherwise at least user_name should have been set and the comment</span>
    <span class="c"># was posted by a non-authenticated user.</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">),</span>
                    <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;</span><span class="si">%(class)s</span><span class="s">_comments&quot;</span><span class="p">)</span>
    <span class="n">user_name</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;user&#39;s name&quot;</span><span class="p">),</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">user_email</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">EmailField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;user&#39;s email address&quot;</span><span class="p">),</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">user_url</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">URLField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;user&#39;s URL&quot;</span><span class="p">),</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

    <span class="n">comment</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;comment&#39;</span><span class="p">),</span> <span class="n">max_length</span><span class="o">=</span><span class="n">COMMENT_MAX_LENGTH</span><span class="p">)</span>

    <span class="c"># Metadata about the comment</span>
    <span class="n">submit_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;date/time submitted&#39;</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
    <span class="n">ip_address</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">GenericIPAddressField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;IP address&#39;</span><span class="p">),</span> <span class="n">unpack_ipv4</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">blank</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">null</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">is_public</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;is public&#39;</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
                    <span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Uncheck this box to make the comment effectively &#39;</span> \
                                <span class="s">&#39;disappear from the site.&#39;</span><span class="p">))</span>
    <span class="n">is_removed</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;is removed&#39;</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
                    <span class="n">help_text</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Check this box if the comment is inappropriate. &#39;</span> \
                                <span class="s">&#39;A &quot;This comment has been removed&quot; message will &#39;</span> \
                                <span class="s">&#39;be displayed instead.&#39;</span><span class="p">))</span>

    <span class="c"># Manager</span>
    <span class="n">objects</span> <span class="o">=</span> <span class="n">CommentManager</span><span class="p">()</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">db_table</span> <span class="o">=</span> <span class="s">&quot;django_comments&quot;</span>
        <span class="n">ordering</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;submit_date&#39;</span><span class="p">,)</span>
        <span class="n">permissions</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&quot;can_moderate&quot;</span><span class="p">,</span> <span class="s">&quot;Can moderate comments&quot;</span><span class="p">)]</span>
        <span class="n">verbose_name</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;comment&#39;</span><span class="p">)</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;comments&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s">: </span><span class="si">%s</span><span class="s">...&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment</span><span class="p">[:</span><span class="mi">50</span><span class="p">])</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">submit_date</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">submit_date</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Comment</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_userinfo</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get a dictionary that pulls together information about the poster</span>
<span class="sd">        safely for both authenticated and non-authenticated comments.</span>

<span class="sd">        This dict will have ``name``, ``email``, and ``url`` fields.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s">&quot;_userinfo&quot;</span><span class="p">):</span>
            <span class="n">userinfo</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s">&quot;name&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_name</span><span class="p">,</span>
                <span class="s">&quot;email&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_email</span><span class="p">,</span>
                <span class="s">&quot;url&quot;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_url</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_id</span><span class="p">:</span>
                <span class="n">u</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">user</span>
                <span class="k">if</span> <span class="n">u</span><span class="o">.</span><span class="n">email</span><span class="p">:</span>
                    <span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;email&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">email</span>

                <span class="c"># If the user has a full name, use that for the user name.</span>
                <span class="c"># However, a given user_name overrides the raw user.username,</span>
                <span class="c"># so only use that if this comment has no associated name.</span>
                <span class="k">if</span> <span class="n">u</span><span class="o">.</span><span class="n">get_full_name</span><span class="p">():</span>
                    <span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">get_full_name</span><span class="p">()</span>
                <span class="k">elif</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_name</span><span class="p">:</span>
                    <span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">u</span><span class="o">.</span><span class="n">get_username</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_userinfo</span> <span class="o">=</span> <span class="n">userinfo</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_userinfo</span>
    <span class="n">userinfo</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_userinfo</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="n">_get_userinfo</span><span class="o">.</span><span class="n">__doc__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_name</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;name&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_set_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_id</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;This comment was posted by an authenticated &quot;</span>\
                                   <span class="s">&quot;user and thus the name is read-only.&quot;</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_name</span> <span class="o">=</span> <span class="n">val</span>
    <span class="n">name</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_name</span><span class="p">,</span> <span class="n">_set_name</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;The name of the user who posted this comment&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_email</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;email&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_set_email</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_id</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&quot;This comment was posted by an authenticated &quot;</span>\
                                   <span class="s">&quot;user and thus the email is read-only.&quot;</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_email</span> <span class="o">=</span> <span class="n">val</span>
    <span class="n">email</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_email</span><span class="p">,</span> <span class="n">_set_email</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;The email of the user who posted this comment&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">userinfo</span><span class="p">[</span><span class="s">&quot;url&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">_set_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">val</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_url</span> <span class="o">=</span> <span class="n">val</span>
    <span class="n">url</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">_get_url</span><span class="p">,</span> <span class="n">_set_url</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="s">&quot;The URL given by the user who posted this comment&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_absolute_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">anchor_pattern</span><span class="o">=</span><span class="s">&quot;#c</span><span class="si">%(id)s</span><span class="s">&quot;</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_content_object_url</span><span class="p">()</span> <span class="o">+</span> <span class="p">(</span><span class="n">anchor_pattern</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">__dict__</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_as_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return this comment as plain text.  Useful for emails.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">d</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s">&#39;user&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">user</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
            <span class="s">&#39;date&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">submit_date</span><span class="p">,</span>
            <span class="s">&#39;comment&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment</span><span class="p">,</span>
            <span class="s">&#39;domain&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">domain</span><span class="p">,</span>
            <span class="s">&#39;url&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_absolute_url</span><span class="p">()</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;Posted by </span><span class="si">%(user)s</span><span class="s"> at </span><span class="si">%(date)s</span><span class="se">\n\n</span><span class="si">%(comment)s</span><span class="se">\n\n</span><span class="s">http://</span><span class="si">%(domain)s%(url)s</span><span class="s">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">d</span>

</div>
<span class="nd">@python_2_unicode_compatible</span>
<span class="k">class</span> <span class="nc">CommentFlag</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Records a flag on a comment. This is intentionally flexible; right now, a</span>
<span class="sd">    flag could be:</span>

<span class="sd">        * A &quot;removal suggestion&quot; -- where a user suggests a comment for (potential) removal.</span>

<span class="sd">        * A &quot;moderator deletion&quot; -- used when a moderator deletes a comment.</span>

<span class="sd">    You can (ab)use this model to add other flags, if needed. However, by</span>
<span class="sd">    design users are only allowed to flag a comment with a given flag once;</span>
<span class="sd">    if you want rating look elsewhere.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">user</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_USER_MODEL</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">),</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;comment_flags&quot;</span><span class="p">)</span>
    <span class="n">comment</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">ForeignKey</span><span class="p">(</span><span class="n">Comment</span><span class="p">,</span> <span class="n">verbose_name</span><span class="o">=</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;comment&#39;</span><span class="p">),</span> <span class="n">related_name</span><span class="o">=</span><span class="s">&quot;flags&quot;</span><span class="p">)</span>
    <span class="n">flag</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;flag&#39;</span><span class="p">),</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">30</span><span class="p">,</span> <span class="n">db_index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
    <span class="n">flag_date</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;date&#39;</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

    <span class="c"># Constants for flag types</span>
    <span class="n">SUGGEST_REMOVAL</span> <span class="o">=</span> <span class="s">&quot;removal suggestion&quot;</span>
    <span class="n">MODERATOR_DELETION</span> <span class="o">=</span> <span class="s">&quot;moderator deletion&quot;</span>
    <span class="n">MODERATOR_APPROVAL</span> <span class="o">=</span> <span class="s">&quot;moderator approval&quot;</span>

    <span class="k">class</span> <span class="nc">Meta</span><span class="p">:</span>
        <span class="n">db_table</span> <span class="o">=</span> <span class="s">&#39;django_comment_flags&#39;</span>
        <span class="n">unique_together</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="s">&#39;comment&#39;</span><span class="p">,</span> <span class="s">&#39;flag&#39;</span><span class="p">)]</span>
        <span class="n">verbose_name</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;comment flag&#39;</span><span class="p">)</span>
        <span class="n">verbose_name_plural</span> <span class="o">=</span> <span class="n">_</span><span class="p">(</span><span class="s">&#39;comment flags&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> flag of comment ID </span><span class="si">%s</span><span class="s"> by </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> \
            <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flag</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">comment_id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">get_username</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">flag_date</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">flag_date</span> <span class="o">=</span> <span class="n">timezone</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">CommentFlag</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../../index.html">Module code</a>
        
          <ul><li><a href="../../../django.html">django</a>
        
          <ul><li><a href="../comments.html">django.contrib.comments</a>
        
        <ul><li>django.contrib.comments.models</li></ul>
        </li></ul></li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>